import { request } from 'umi';

// 获取流量数据列表
export async function getTrafficList(params: {
  page?: number;
  pageSize?: number;
  startDate?: string;
  endDate?: string;
  source?: string;
  medium?: string;
  campaign?: string;
}) {
  return request('/api/traffic', {
    method: 'GET',
    params,
  });
}

// 获取流量数据汇总
export async function getTrafficSummary(params: {
  startDate: string;
  endDate: string;
}) {
  return request('/api/traffic/summary', {
    method: 'GET',
    params,
  });
}

// 获取流量来源分布
export async function getSourceDistribution(params: {
  startDate: string;
  endDate: string;
}) {
  return request('/api/traffic/source-distribution', {
    method: 'GET',
    params,
  });
}

// 获取转化率趋势
export async function getConversionTrend(params: {
  startDate: string;
  endDate: string;
}) {
  return request('/api/traffic/conversion-trend', {
    method: 'GET',
    params,
  });
}

// 导出数据
export async function exportData(params: {
  startDate: string;
  endDate: string;
  type?: 'xlsx' | 'csv';
}) {
  return request('/api/traffic/export', {
    method: 'GET',
    params,
    responseType: 'blob',
  });
}

// 导入Excel数据
export async function importExcel(file: File) {
  const formData = new FormData();
  formData.append('file', file);

  return request('/api/traffic/import', {
    method: 'POST',
    data: formData,
    requestType: 'form',
  });
}

// 创建流量数据
export async function createTrafficData(data: any[]) {
  return request('/api/traffic', {
    method: 'POST',
    data,
  });
}

// 批量更新流量数据
export async function batchUpdateTrafficData(ids: (string | number)[], data: any) {
  return request('/api/traffic/batch', {
    method: 'PUT',
    data: { ids, data },
  });
}

// 更新流量数据
export async function updateTrafficData(id: string | number, data: any) {
  return request(`/api/traffic/${id}`, {
    method: 'PUT',
    data,
  });
}

// 批量删除流量数据
export async function batchDeleteTrafficData(ids: (string | number)[]) {
  return request('/api/traffic/batch', {
    method: 'DELETE',
    data: { ids },
  });
}

// 删除流量数据
export async function deleteTrafficData(id: string | number) {
  return request(`/api/traffic/${id}`, {
    method: 'DELETE',
  });
} 